package com.hxy.health.platform.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxy.health.platform.dao.CartMapper;
import com.hxy.health.platform.pojo.Cart;
import com.hxy.health.platform.pojo.Product;
import com.hxy.health.platform.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;

    /**
     * 更新购物车
     * @param product
     * @param quantity
     * @param email
     * @return
     */
    @Override
    public int updateCart(Product product, Integer quantity, String email) {
        // 先去购物车里面查找是否存在
        Cart result = cartMapper.getByEmailAndProductId(product.getId(), email);
        if (result != null) {
            Cart cart = new Cart();
            cart.setId(result.getId());
            cart.setQuantity(result.getQuantity() + quantity);
            cart.setProductTotalPrice(String.valueOf(Double.parseDouble(result.getProductTotalPrice()) + (Double.parseDouble(product.getNowPrice()) * quantity)));
            return cartMapper.updateByPrimaryKeySelective(cart);
        }
        Cart cart = new Cart();
        cart.setEmail(email);
        cart.setProductId(product.getId());
        cart.setProductName(product.getTitle());
        cart.setProductPrice(product.getNowPrice());
        cart.setQuantity(1);
        cart.setProductImg(product.getImgUrl());
        cart.setProductTotalPrice(product.getNowPrice());
        return cartMapper.insertSelective(cart);
    }

    /**
     * 根据邮件获取集合
     * @param email
     * @return
     */
    @Override
    public PageInfo<Cart> getByEmail(Integer pageNum, Integer pageSize, String email) {
        PageHelper.startPage(pageNum, pageSize);
        List<Cart> carts = cartMapper.getByEmail(email);
        PageInfo<Cart> cartPageInfo = new PageInfo(carts);
        return cartPageInfo;
    }

    /**
     * 根据ID删除购物车
     * @param cartId
     * @param
     * @return
     */
    @Override
    public int deleteCartById(Integer cartId) {

        return cartMapper.deleteByPrimaryKey(cartId);
    }

    /**
     * 动态添加
     * @param productId
     * @param quantity
     * @return
     */
    @Override
    public int updateTotalPrice(Integer productId, Integer quantity, Double productPrice) {
        Cart cart = new Cart();
        cart.setId(productId);
        cart.setQuantity(quantity);
        cart.setProductTotalPrice(String.valueOf(productPrice * quantity));
        return cartMapper.updateByPrimaryKeySelective(cart);

    }

    /**
     * 获取购物车明细
     * @param email
     * @return
     */
    @Override
    public List<Cart> getCartByEmail(String email) {
        return cartMapper.getByEmail(email);
    }

    /**
     * 清空购物车
     * @param email
     */
    @Override
    public void deleteByEmail(String email) {
        cartMapper.deleteByEmail(email);
    }
}
